package com.ferret.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ferret.bean.dto.*;
import com.ferret.bean.entity.Camera;
import com.ferret.bean.entity.VIIDSubscribe;
import com.ferret.bean.vo.FaceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class VIIDNotificationService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private VIIDSubscribeService viidSubscribeService;

    public VIIDSubscribe getSubscribeById(String subscribeId) {
        return viidSubscribeService.getCacheById(subscribeId);
    }

    public ResponseStatusObject motorVehicleHandler(SubscribeNotificationObject notificationObject, VIIDSubscribe subscribe) {
        return new ResponseStatusObject(null, "0", "操作成功");
    }

    public ResponseStatusObject faceVehicleHandler(SubscribeNotificationObject notificationObject, VIIDSubscribe subscribe) {
        return new ResponseStatusObject(null, "0", "操作成功");
    }

    public ResponseStatusObject tollgateHandler(SubscribeNotificationObject notificationObject, VIIDSubscribe subscribe) {

        return new ResponseStatusObject(null, "0", "操作成功");
    }

    public ResponseStatusObject apeDeviceHandler(SubscribeNotificationObject notificationObject) {
        DeviceObjectList deviceList = notificationObject.getDeviceList();
        List<APEObject> apeObjects = deviceList.getAPEObject();
        List<Camera> cameraList = new ArrayList<>();
        for (int i = 0; i < apeObjects.size(); i++) {
            Camera camera = new Camera();
            APEObject apeObject = apeObjects.get(i);
            camera.setCameraid(apeObject.getApeID());
            camera.setCameraname(apeObject.getName());
            camera.setType(apeObject.getModel());
            camera.setCameraip(apeObject.getIPAddr());
            camera.setPort(Convert.toInt(apeObject.getPort()));
            camera.setX(Convert.toStr(apeObject.getLongitude()));
            camera.setY(Convert.toStr(apeObject.getLatitude()));
            camera.setPlaceId(apeObject.getPlaceCode());
            camera.setIsenable(Convert.toInt(apeObject.getIsOnline()));
            camera.setUsername(apeObject.getUserId());
            camera.setPwd(apeObject.getPassword());
            cameraList.add(camera);
        }
        return new ResponseStatusObject(null, "0", "操作成功");
    }

    public ResponseStatusObject faceHandler(SubscribeNotificationObject notificationObject) throws Exception {
        String workerPath = System.getProperty("user.dir");
        FaceObjectList faceObjectList = notificationObject.getFaceObjectList();
        List<FaceObject> faceObjects = faceObjectList.getFaceObject();
        for (int j = 0; j < faceObjects.size(); j++) {
            JSONArray jsonArray = JSONUtil.createArray();
            FaceObject faceObject = faceObjects.get(j);
            String deviceId = faceObject.getDeviceID();
//            Camera camera = cameraDao.findByCameraid(deviceId);
//            if (ObjUtil.isNotNull(camera) && StrUtil.isNotBlank(camera.getCameraid())) {
        }
        return new ResponseStatusObject(null, "0", "操作成功");
    }

    public ResponseStatusObject nonMotorVehicleHandler(SubscribeNotificationObject notificationObject, VIIDSubscribe subscribe) {

        return new ResponseStatusObject(null, "0", "操作成功");
    }

    public ResponseStatusObject personHandler(SubscribeNotificationObject notificationObject, VIIDSubscribe subscribe) {
        return new ResponseStatusObject(null, "0", "操作成功");
    }
}
